package gui.view.android;

import gui.view.MetroView;

import java.awt.GridLayout;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;

import javax.swing.ButtonGroup;
import javax.swing.JCheckBox;
import javax.swing.JPopupMenu;
import javax.swing.JRadioButtonMenuItem;

import model.MetroModel;

import com.nightpiter.controller.log.Logger;
import com.nightpiter.view.drawer.MetroDrawer;

public class AndroidDrawerView extends MetroView{
	private AndroidDrawerPanel drawerPanel;
	
	private Point lastDragPoint;
	/**/
	public AndroidDrawerView() {
		drawerPanel = new AndroidDrawerPanel(); 
		this.setLayout(new GridLayout());
		this.add(drawerPanel);
		
		this.addMouseListener(new MapScrollAuxiliaryListener());
		this.addMouseMotionListener(new MapScrollListener());
		this.addMouseWheelListener(new MapScaleListener());
		this.addMouseListener(new PopupMenuListener());
		
		
	}
	
	
	
	@Override
	public void modelChanged(boolean newObjectsAdded) {
		drawerPanel.updateModel();		
		
		drawerPanel.repaint();		
	}
	
	
	private class MapScaleListener implements MouseWheelListener {
		private final float SCALE_STEP = 0.1f;
		
		@Override
		public void mouseWheelMoved(MouseWheelEvent arg0) {
			float scaleShift = (arg0.getWheelRotation()<0) ? SCALE_STEP : -SCALE_STEP;
			drawerPanel.setScale(drawerPanel.getScale() + scaleShift);
			//drawerPanel.setCenter((int)(drawerPanel.getCenter().x/drawerPanel.getScale()),(int)(drawerPanel.getCenter().y/drawerPanel.getScale()));
			drawerPanel.repaint();
			
		}
		
	}
	private class MapScrollListener implements MouseMotionListener {
		public void mouseDragged(MouseEvent arg0) {
			
			
			
			int dx = arg0.getPoint().x -lastDragPoint.x; 
			int dy = arg0.getPoint().y -lastDragPoint.y;
			drawerPanel.shift(dx, dy);
			lastDragPoint = new Point(arg0.getPoint().x,arg0.getPoint().y);
			
			drawerPanel.repaint();
		}

		public void mouseMoved(MouseEvent arg0) {
			
			
		}

	}
	private class MapScrollAuxiliaryListener implements MouseListener {

		@Override
		public void mouseClicked(MouseEvent arg0) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void mouseEntered(MouseEvent arg0) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void mouseExited(MouseEvent arg0) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void mousePressed(MouseEvent arg0) {
			lastDragPoint = new Point(arg0.getPoint().x, arg0.getPoint().y);
			
		}

		@Override
		public void mouseReleased(MouseEvent arg0) {
			lastDragPoint = null;
			
		}
	}
	
	class AndroidViewPopupMenu extends JPopupMenu {
		AndroidViewPopupMenu() {
			ButtonGroup drawersGroup = new ButtonGroup();
			for(AndroidDrawersRegistry.DrawerStruct dStruct:AndroidDrawersRegistry.getDrawers()) {
				final MetroDrawer drawer = dStruct.drawer;
				JRadioButtonMenuItem rbMenuItem;				
				rbMenuItem = new JRadioButtonMenuItem(dStruct.name);
				rbMenuItem.addActionListener(new ActionListener() {

					@Override
					public void actionPerformed(ActionEvent e) {
						drawerPanel.setDrawer(drawer);
						
					}
					
				});
				if(drawerPanel.getDrawer()==dStruct.drawer) rbMenuItem.setSelected(true);				
				drawersGroup.add(rbMenuItem);
				add(rbMenuItem);
			}
		}
	}
	
	class PopupMenuListener extends MouseAdapter {

		  

		  public PopupMenuListener() {		    
		  }

		  public void mouseReleased(MouseEvent mouseEvent) {
			  if(mouseEvent.isPopupTrigger())
				  new AndroidViewPopupMenu().show(mouseEvent.getComponent(), mouseEvent.getX(), mouseEvent
			          .getY());
		  }
		}
	@Override
	public void afterPanelInit() {
		drawerPanel.reset();
		Rectangle mapBounds = getMapBounds();
		drawerPanel.shift(this.getWidth()/2, this.getHeight()/2);
		float scaleX = (float)(this.getHeight()/mapBounds.getHeight());
		float scaleY = (float)(this.getWidth()/mapBounds.getWidth());
		float finalScale = Math.min(scaleX, scaleY);
		drawerPanel.setScale(finalScale);
		//drawer.setDrawerCenter(0, 0);
		drawerPanel.shift(-(int)(mapBounds.getCenterX()*finalScale), -(int)(mapBounds.getCenterY()*finalScale));
		//Logger.log(mapBounds.getMinX()*finalScale+"");*/
		
	}	
}
